class Match {
    constructor(s, r) {
        String.prototype.has = function (str) {
            return this.split('').some((v, i) => {
                return v === str
            })
        }
        String.prototype.getArray = function() {
            return this.split('')
        }
        Array.prototype.getString = function () {
            return this.join('')
        }
        this.s = s
        this.r = r
        this.count1 = 0 
        this.count2 = 0
    }
    // 获取 * 号的索引值
    getStarIndex() {
        return this.r.split('').map((v, i) => {
            if(v === '*') {
                return i
            }else{
                return false
            }
        }).filter((v, i) => {
            return v
        })
    }

    //获取 参数s 的改变时的索引值
    getsChangeIndex() {
        let result = [0]
        let arr = this.s.getArray()
        for(let i = 0; i < this.s.length; i++) {
            if(arr[i] != arr[i+1]) {
                result.push(i + 1)
            }
        }
        return result
    }
    //获取 参数r 的改变时的索引值
    getrChangeIndex() {
        let result = [0]
        let arr = this.r.getArray()
        for(let i = 0; i < this.r.length; i++) {
            if (arr[i] === '*') continue
            if(arr[i + 1] === '*') {
                if(arr[i] !== arr[i + 2]) {
                    result.push(i + 2)
                }
            }else{
                if(arr[i] !== arr[i + 1]) {
                    result.push(i + 1)
                }
            }
        }
        return result
    }

    getStarNumber(str) {
        return str.getArray().filter((v, i) => {
            return v === '*'
        }).length
    }
    findStarBeforeAndBehand() {
        const starIndex = this.getStarIndex()

    }
    passed() {
        this.count1 += 1
        this.count2 += 1
    }
    match() {
        const sNumber = this.getsChangeIndex()
        const rNumber = this.getrChangeIndex()
        for(let i = 0;i < sNumber.length - 1; i++) {
            const s = this.s.getArray().slice(this.getsChangeIndex()[this.count1], this.getsChangeIndex()[this.count1 + 1]).getString()
            const r = this.r.getArray().slice(this.getrChangeIndex()[this.count2], this.getrChangeIndex()[this.count2 + 1]).getString()
            const atLeast = r.length - this.getStarNumber(r) * 2
            if(r[0] === '.' &&  r.has('*') && s.length > atLeast) {
                this.passed()
                continue
            }
            if(r[0] === '.' && !r.has('*') && s.length === atLeast) {
                this.passed()
                continue
            }
            if(s[0] !== r[0] && r.has('*') && atLeast === 0) {
                this.count2 += 1
                i -= 1
                continue
            }
            if(r.has('*') && s.length >= atLeast) {
                this.passed()
                continue
            }
            if(!r.has('*') && s.length === atLeast) {
                this.passed()
                continue
            }
            return false

        }
        if(this.getrChangeIndex().length - this.count2 === 1) {
            return true
        }else{
            return false
        }
        
    }

}

// const isMatch = new Match('aaaabbbvvvv', 'a*a*abbbc*v*')  true
// const isMatch = new Match('aaaabbbvvvv', 'a*a*abbbfv*')  // false
// const isMatch = new Match('aaaabbbvvvv', 'a*a*abb*.*.*')  // true
console.log(isMatch.match())